﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYDuctCross : HYDuctTee
	{
		public double Side2Flow { get; set; }

		public HYMEPObject Side2
		{
			get
			{
				return this.m_Side2Object;
			}
		}

		public int Side2ObjectId
		{
			get
			{
				if (this.m_Side2Object != null)
				{
					this.m_Side2ObjectId = this.m_Side2Object.Id;
				}
				return this.m_Side2ObjectId;
			}
		}

		public double Side2Cofficient
		{
			get
			{
				if (Utility.IsZero(this.m_UserSide2Coefficient, 1E-05))
				{
					return this.m_Side2Coefficient;
				}
				return this.m_UserSide2Coefficient;
			}
			set
			{
				this.m_Side2Coefficient = value;
			}
		}

		public HYDuctSpecification Side2Port
		{
			get
			{
				return this.m_Side2Port;
			}
			set
			{
				this.m_Side2Port = value;
			}
		}

		public double Side2Angle { get; set; }

		public string Side2CoefficientShowString { get; set; }

		public HYDuctCross()
		{
			base.Name = "四通";
			this.m_Side2Port = new HYDuctSpecification(HYShapeProfile.Rectangular, 800, 320);
			this.Side2Angle = Math.PI*.5;
		}

		public void SetSide2Object(HYMEPObject mepObj)
		{
			if (mepObj == null)
			{
				return;
			}
			this.m_Side2Object = mepObj;
			this.m_Side2ObjectId = this.m_Side2Object.Id;
			this.m_Side2Object.Previous = this;
			this.m_Side2Object.Place = HYMEPObject.Down(base.Place);
			if (this.m_Side2Object is HYPipeline)
			{
				this.m_Side2HostPipeline = (this.m_Side2Object as HYPipeline);
			}
		}

		public virtual HYPipeline GetSide2HostPipeline()
		{
			if (this.m_Side2HostPipeline != null)
			{
				return this.m_Side2HostPipeline;
			}
			HYMEPObject hymepobject = this.Side2;
			while (hymepobject != null && !(hymepobject is HYPipeline))
			{
				hymepobject = hymepobject.Next;
			}
			if (hymepobject == null)
			{
				return null;
			}
			this.m_Side2HostPipeline = (hymepobject as HYPipeline);
			return this.m_Side2HostPipeline;
		}

		public override void SetCoeffient(double coef, int PartIndex = 0)
		{
			if (2 == PartIndex)
			{
				this.m_UserSide2Coefficient = coef;
				return;
			}
			base.SetCoeffient(coef, PartIndex);
		}

		protected override double CalDirectCoefficient()
		{
			HYDuctStraight hyductStraight = this.GetHostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight2 = this.GetSideHostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight3 = this.GetSide2HostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight4 = this.GetPreviousPipeline() as HYDuctStraight;
			if (base.IsConfluence)
			{
				if (hyductStraight2.Flow > hyductStraight3.Flow)
				{
					return Hydrokinetics.HeLiuSanTongDragLocalIndex(hyductStraight4.Velocity, hyductStraight4.Flow, hyductStraight.Velocity, hyductStraight.Flow, hyductStraight2.Velocity, hyductStraight2.Flow, base.SideAngle, this.Side2Angle, false);
				}
				return Hydrokinetics.HeLiuSanTongDragLocalIndex(hyductStraight4.Velocity, hyductStraight4.Flow, hyductStraight.Velocity, hyductStraight.Flow, hyductStraight3.Velocity, hyductStraight3.Flow, this.Side2Angle, base.SideAngle, false);
			}
			else
			{
				if (base.IsRound)
				{
					return Hydrokinetics.DistributaryCLR_90Circle_S(hyductStraight4.Flow, hyductStraight4.Section.Area, hyductStraight.Flow, hyductStraight.Section.Area);
				}
				return Hydrokinetics.DistributaryCLR_90Rectangle_S(hyductStraight4.Flow, hyductStraight4.Section.Area, hyductStraight.Flow, hyductStraight.Section.Area);
			}
		}

		public override void CalCoefficient()
		{
			base.CalCoefficient();
			HYDuctStraight plSide = this.GetSide2HostPipeline() as HYDuctStraight;
			if (Utility.IsZero(this.m_UserSide2Coefficient, 1E-05))
			{
				this.m_Side2Coefficient = this.CalSideCoefficient(plSide);
			}
		}

		public override void Cal(HYSystemOption option)
		{
			HYPipeline side2HostPipeline = this.GetSide2HostPipeline();
			if (side2HostPipeline == null)
			{
				return;
			}
			base.Cal(option);
			this.Side2CoefficientShowString = "【" + base.Name + "】旁通管2";
			side2HostPipeline.AddAttachedObject(this, this.Side2CoefficientShowString, this.Side2Cofficient, false, 2);
		}

		public override void RebuildIndex(ref int index)
		{
			base.RebuildIndex(ref index);
			if (this.m_Side2Object != null)
			{
				this.m_Side2Object.RebuildIndex(ref index);
			}
		}

		public override void ReAllocateFlow()
		{
			base.ReAllocateFlow();
			double num = 0.0;
			if (this.m_Side2Object != null)
			{
				this.m_Side2Object.ReAllocateFlow();
				num = this.m_Side2Object.Flow;
			}
			base.Flow += num;
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.Side2Cofficient = xe.GetDoubleAttribute("Side2Cofficient", 0.0);
			this.m_Side2ObjectId = xe.GetIntAttribute("Side2ObjectId", 0);
			XElement xe2 = xe.ChildElement("Side2");
			this.m_Side2Port.ReadXml(xe2);
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("Side2Cofficient", this.Side2Cofficient);
			xe.SetAttributeValue("Side2ObjectId", this.Side2ObjectId);
			XElement xe2 = xe.ChildElement("Side2");
			this.m_Side2Port.WriteXml(xe2);
		}

		protected HYDuctSpecification m_Side2Port;

		protected int m_Side2ObjectId;

		protected HYMEPObject m_Side2Object;

		protected HYPipeline m_Side2HostPipeline;

		protected double m_Side2Coefficient;

		protected double m_UserSide2Coefficient;
	}
}
